<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);

// Basic parameter data
$mode		= request_var('mode', '');

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];

if ($user_id == ANONYMOUS)
{
	sv_login_box();
}

display_forums('', $config['load_moderators']);

if ($mode == 'short_cover')
{	
	$corp_id	= $db->sql_escape(request_var('corp_id', ''));	
	$number_name= $corp_id . '_number';	
	$number		= $db->sql_escape(request_var($number_name, ''));	
	$share		= (int)($number) * TRANSACTION_UNIT;
	$note		= $db->sql_escape(utf8_clean_string(request_var('note', '', true)));
    
	$request_price	= $db->sql_escape(request_var('request_price', ''));	
	
	if (is_valid_corp_id($corp_id) && $share > 0)
	{
		// check if the user has enough shares of the corp to sell.
		$sql 	= 'SELECT * FROM sv_short_stock WHERE user_id="' . $user_id . '" AND corp_id="' . $corp_id . '"';
		$result = $db->sql_query_limit($sql, 1); 
		if ($row = $db->sql_fetchrow($result))
		{
			if ((int)$row['share'] - (int)$row['share_to_short_cover'] >= $share)
			{		
				$db->sql_transaction('begin');
				// Add to transaction list
				$sql = 'INSERT INTO sv_transaction ' . $db->sql_build_array('INSERT', array(
							'user_id'		=> (int) $user->data['user_id'],
							'corp_id'		=> $corp_id,
							'action'		=> 'to_short_cover',
							'share'			=> (int) $share,
							'price'			=> (float) 0.0,
							'request_price'	=> $request_price,
							'request_time'	=> get_taiwan_time(),
							'note'			=> $note,
							'deal_time'		=> '')
						);
				$db->sql_query($sql);
	
				// Update holding stock - share_to_sell
				$sql = 'UPDATE sv_short_stock '. "SET share_to_short_cover = share_to_short_cover + $share	WHERE user_id = $user_id AND corp_id = ". '"' . $corp_id . '"';
				$db->sql_query($sql);
	
				$db->sql_transaction('commit');
			}
		}
	}
}
else if ($mode == 'delete_delegation')
{		
	$trans_id	= (int)(request_var('trans_id', ''));
	delete_delegation($trans_id);
}

$short_stock_not_empty = false;

// list all holding stock for the user to sell. 
$sql 	= 'SELECT * FROM sv_short_stock WHERE user_id=' . $user_id;
$result = $db->sql_query($sql); 
while ($row = $db->sql_fetchrow($result))
{
	if (is_valid_corp_id($row['corp_id']) == false)
	{
		continue;
	}
	
	$short_stock_not_empty = true;
	// Send vars to template
	$template->assign_block_vars('short_stock_list', array(
		'CORP_ID'						=> $row['corp_id'],
		'SHORT_COVERABLE_NUMBER'		=> (int)(((int)$row['share'] - (int)$row['share_to_short_cover'])/TRANSACTION_UNIT),
		'HOLDING_NUMBER'				=> (int)$row['share']/TRANSACTION_UNIT,
		'TO_SHORT_COVER_NUMBER'			=> (int)$row['share_to_short_cover']/TRANSACTION_UNIT,
		'CORP_NAME'						=> get_corp_chinese_name($row['corp_id']),
		'CANNOT_SELL'					=> ((int)$row['share'] > (int)$row['share_to_short_cover']) ? false : true ,
		'COST'							=> $row['cost'],
		'REFERED_PRICE'					=> get_refered_price($row['corp_id']),
		'DEPOSIT'						=> number_format($row['deposit']),		
		)
	);
}

$to_short_cover_delegation_list = false;
// list all open to_short cover delegation. 
list_open_to_short_cover_delegation();

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'									=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_SHORT_COVER_ACTION'								=> append_sid("{$phpbb_root_path}short_cover.$phpEx", 'mode=short_cover'),
	'S_DELETE_TO_SHORT_COVER_DELEGATION_ACTION'			=> append_sid("{$phpbb_root_path}short_cover.$phpEx", 'mode=delete_delegation'),
	
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'S_TO_BUY_DELEGATION_LIST'	=> $to_buy_delegation_list,
	'S_TO_SELL_DELEGATION_LIST'	=> $to_sell_delegation_list,
	'S_SHORT_STOCK_LIST'		=> $short_stock_not_empty,
	'U_MONEY'					=> number_format($money),
	
	'S_ALLOW_DELETE_DELEGATION'			=> true,
	'S_TO_SHORT_COVER_DELEGATION_LIST'	=> $to_short_cover_delegation_list,
	
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '')
);

// Output page
page_header($user->lang['SV_SHORT_COVER']);

$template->set_filenames(array(
	'body' => 'short_cover.html')
);

page_footer();

?>